package light.head.dao.site;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.Commodity;
import light.head.model.CommodityImg;
import light.head.model.FileInfo;

import org.springframework.stereotype.Service;

/**
 * 商品
 */
@Service
public class CommodityDAO extends BaseDAO<Commodity, Serializable> {
	private static final String sql_findAnnex = "FROM CommodityImg ci WHERE ci.cc = ?";
	private static final String sql_findCover = "FROM CommodityImg ci WHERE ci.cc = ? AND ci.cover = 1";
	private static final String sql_getByCategoryCode = "FROM Commodity comm WHERE comm.cc = ?";
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM Commodity c WHERE 1 = 1");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();
			String name = map.get(Parm.NAME);
			String oc = map.get(Parm.OC);
			String allow = map.get(Parm.ALLOW);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String deleted = map.get(Parm.DELETED);
			if (oc != "" && isNotNull(oc)) {
				hql.append(" AND c.oc = ? ");
				list.add(Integer.parseInt(oc));
			}
			if (name != "" && isNotNull(name)) {
				hql.append(" AND c.name LIKE ? ");
				list.add("%" + name + "%");
			}
			if (time1 != "" && isNotNull(time1)) {
				hql.append(" AND c.ct >= ? ");
				list.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
			}
			if (time2 != "" && isNotNull(time2)) {
				hql.append(" AND c.ct <= ?");
				list.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
			}
			if (allow != "" && isNotNull(allow)) {
				hql.append(" AND c.allow = ?");
				list.add(Byte.parseByte(allow));
			}
			if (deleted != "" && isNotNull(deleted)) {
				hql.append(" AND c.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY c.code");

		return findToPageBean(hql.toString(), pageBean, list);
	}

	/**
	 * 根据拼接好的文件编号序列获取对应的文件列表
	 */
	public List<FileInfo> findFileByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM FileInfo f WHERE f.code IN (" + codes + ")";
		List<FileInfo> list = find(sql);
		return list;
	}

	public List<CommodityImg> findAnnexByCode(Integer code) {
		return super.find(sql_findAnnex, code);
	}

	public RESULT deleteAnnexNotInCodes(Integer code, List<Object> codes) {
		StringBuffer sql = new StringBuffer();
		List<Object> parm = new ArrayList<Object>();
		parm.add(code);
		for (Object o : codes) {
			if(o != null){
				sql.append("?,");
				parm.add(o);
			}
		}
		if (sql.length() > 0) {
			sql.insert(0, " AND ca.code NOT IN(")
					.deleteCharAt(sql.length() - 1).append(")");
		}
		sql.insert(0, "DELETE FROM CommodityImg ca WHERE ca.cc = ?");
		execute(sql.toString(), parm.toArray());
		return RESULT.SUCCESS;
	}
	//更具 产品类型获取产品信息
	public List<Commodity> getByCategory(Integer code) {
		return super.find(sql_getByCategoryCode, code);
	}
	//更具产品类型获取产品及产品图片
	public List<Commodity> getByCa(Integer code) {
		List<Commodity> list = getByCategory(code);
		for (int i = 0; i < list.size(); i++) {
			if(list.get(i).getCode() != null){
				list.get(i).setList(findCoverByCode(list.get(i).getCode()));
			}
		}
		return list;
	}
	//获取封面图片
	private  List<CommodityImg> findCoverByCode(Integer code) {
		return super.find(sql_findCover, code);
	}
}
